# 股票爬虫,多线程
import requests
import re
import time
from threading import Thread

import os
__path="E:\\stcokData\\tdx_qfq\\stock_DAY_qfq"
__listdir = os.listdir(__path)

# print(__listdir)
__listcode=[]
for __list in __listdir:
    if __list[0:2]=="SZ":
        __tmplist=__list[3:9]
        __listcode.append(__tmplist)
        # print(__tmplist)
# print(__listcode)
# 股票的代码
stock = ['300657']
    # , '300658', '300659', '300660', '300661', '300662', '300663', '300664', '300665', '300666', '300667',
    #     '300668', '300669', '300670']

# stock=__listcode
m1 = re.compile(r"url: '(.*\..*)'")


# 获取股票的票价信息
def GetPrice(code):
    try:
        url = 'http://quotes.money.163.com/1%s.html' % code
        print(url)
        txt = requests.get(url).text
        # print(txt)
        price = m1.findall(txt)[0]
        print(code, price)
    except:
        pass


if __name__ == "__main__":
    # 创建一个存放线程的列表
    ts = []
    print("开始获取股票的信息....")
    start = time.time()
    for code in stock:
        t = Thread(target=GetPrice, args=(code,))
        ts.append(t)  # 添加到线程列表中
        t.start()  # 同时开启这个线程
    for t in ts:
        t.join()  # 每个线程执行完成后，再继续执行下面的内容
    print("股票信息获取成功！")
    print("耗时：", time.time() - start)